<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>组合数排列算法</title>
  </head>
  <body>
    <script>
      const list = [1, 2, 3, 4]
      /**
       * 组合数排列算法
       * @param {any[]} arr
       * @param {number} size
       * @returns
       */
      function combination(arr, size) {
        let result = []
        function read(temp, list, size) {
          // temp:临时数组 list:目标数组 size：多少个数进行组合
          if (size === 0) {
            result.push(temp)
            return
          }
          for (let i = 0; i <= list.length - size; i++) {
            // 从0开始 到n-m
            let b = temp.slice()
            b.push(list[i])
            read(b, list.slice(i + 1), size - 1)
          }
        }
        read([], arr, size)
        return result
      }
      const result = combination(list, 2)
    </script>
  </body>
</html>
